package com.nmsl.system.presc_temp.service.impl;

import java.util.Collections;
import java.util.List;

import com.nmsl.system.presc_temp.domain.ExaminationInfo;
import com.nmsl.system.presc_temp.domain.PrescriptionMedicine;
import com.nmsl.system.presc_temp.mapper.ExaminationInfoMapper;
import com.nmsl.system.presc_temp.mapper.PrescriptionMedicineMapper;
import com.nmsl.system.presc_temp.mapper.PriscTempMedicalMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.nmsl.system.presc_temp.mapper.PrescriptionTemplateMapper;
import com.nmsl.system.presc_temp.domain.PrescriptionTemplate;
import com.nmsl.system.presc_temp.service.IPrescriptionTemplateService;

/**
 * 处方模板Service业务层处理
 * 
 * @author gmh
 * @date 2025-02-18
 */
@Service
public class PrescriptionTemplateServiceImpl implements IPrescriptionTemplateService 
{
    @Autowired
    private PrescriptionTemplateMapper prescriptionTemplateMapper;
    @Autowired
    private PrescriptionMedicineMapper prescriptionMedicineMapper;
    @Autowired
    private ExaminationInfoMapper examinationInfoMapper;
    @Autowired
    private PriscTempMedicalMapper priscTempMedicalMapper;

    /**
     * 查询处方模板
     * 
     * @param id 处方模板主键
     * @return 处方模板
     */
    @Override
    public PrescriptionTemplate selectPrescriptionTemplateById(Long id)
    {
        return prescriptionTemplateMapper.selectPrescriptionTemplateById(id);
    }

    /**
     * 查询处方模板列表
     * 
     * @param prescriptionTemplate 处方模板
     * @return 处方模板
     */
    @Override
    public List<PrescriptionTemplate> selectPrescriptionTemplateList(PrescriptionTemplate prescriptionTemplate)
    {
        return prescriptionTemplateMapper.selectPrescriptionTemplateList(prescriptionTemplate);
    }

    /**
     * 新增处方模板
     * 
     * @param prescriptionTemplate 处方模板
     * @return 结果
     */
    @Override
    public int insertPrescriptionTemplate(PrescriptionTemplate prescriptionTemplate)
    {
        return prescriptionTemplateMapper.insertPrescriptionTemplate(prescriptionTemplate);
    }

    /**
     * 修改处方模板
     * 
     * @param prescriptionTemplate 处方模板
     * @return 结果
     */
    @Override
    public int updatePrescriptionTemplate(PrescriptionTemplate prescriptionTemplate)
    {
        return prescriptionTemplateMapper.updatePrescriptionTemplate(prescriptionTemplate);
    }

    /**
     * 批量删除处方模板
     * 
     * @param ids 需要删除的处方模板主键
     * @return 结果
     */
    @Override
    public int deletePrescriptionTemplateByIds(Long[] ids)
    {
        for (Long id : ids) {
            prescriptionTemplateMapper.selectPrescriptionInfoId(id);
            if (prescriptionTemplateMapper.selectPrescriptionInfoId(id) > 0) {
                prescriptionMedicineMapper.deletePrescriptionMedicine(prescriptionTemplateMapper.selectPrescriptionInfoId(id));
                priscTempMedicalMapper.deleteByPrescTempId(prescriptionTemplateMapper.selectPrescriptionInfoId(id));
            }
            prescriptionTemplateMapper.selectExaminationInfoId(id);
            if (prescriptionTemplateMapper.selectExaminationInfoId(id) > 0) {
              examinationInfoMapper.deleteExaminationInfo(prescriptionTemplateMapper.selectExaminationInfoId(id));
            }
        }

        return prescriptionTemplateMapper.deletePrescriptionTemplateByIds(ids);
    }

    /**
     * 删除处方模板信息
     * 
     * @param id 处方模板主键
     * @return 结果
     */
    @Override
    public int deletePrescriptionTemplateById(Long id)
    {
        return prescriptionTemplateMapper.deletePrescriptionTemplateById(id);
    }

    @Override
    public List<PrescriptionMedicine> selectPrescriptionMedicineById(Long id) {
        return prescriptionTemplateMapper.selectPrescriptionMedicineListById(id);
    }

    @Override
    public List<ExaminationInfo> selectExaminationInfoById(Long id) {
        return prescriptionTemplateMapper.selectExaminationInfoListById(id);
    }

    @Override
    public Long selectMedId(Long id) {
        System.out.println(prescriptionTemplateMapper.selectPrescriptionInfoId(id));
            return prescriptionTemplateMapper.selectPrescriptionInfoId(id);
    }

    @Override
    public Long selectExamId(Long id) {
            return prescriptionTemplateMapper.selectExaminationInfoId(id);


    }


}
